<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Choosing a Reactor and GUI Toolkit Integration</title>
  </head>
  <body>
    <h1>Choosing a Reactor and GUI Toolkit Integration</h1>

    <h2>Overview</h2>

    <p>Twisted provides a variety of implementations of the <code
    class="API">twisted.internet.reactor</code>.  The specialized
    implementations are suited for different purposes and are
    designed to integrate better with particular platforms.</p>

    <p>The general purpose reactor implementations are:</p>

    <ul>
      <li><a href="#select">The select()-based reactor</a></li>
      <li><a href="#poll">The poll()-based reactor</a></li>
    </ul>

    <p>Platform-specific reactor implementations exist for:</p>

    <ul>
      <li><a href="#kqueue">KQueue for FreeBSD and OS X</a></li>
      <li><a href="#win32_wfmo">Win32 (WFMO)</a></li>
      <li><a href="#win32_iocp">Win32 (IOCP)</a></li>
      <li><a href="#cfreactor">Mac OS X</a></li>
      <li><a href="#epoll">Epoll for Linux 2.6</a></li>
    </ul>

    <p>The remaining custom reactor implementations provide support
    for integrating with the native event loops of various graphical
    toolkits.  This lets your Twisted application use all of the
    usual Twisted APIs while still being a graphical application.</p>

    <p>Twisted currently integrates with the following graphical
    toolkits:</p>

    <ul>
      <li><a href="#gtk">GTK+ 1.2 and 2.0</a></li>
      <li><a href="#tkinter">Tkinter</a></li>
      <li><a href="#wxpython">WxPython</a></li>
      <li><a href="#win32_wfmo">Win32</a></li>
      <li><a href="#cfreactor">CoreFoundation</a></li>
      <li><a href="#pyui">PyUI</a></li>
    </ul>

    <p>When using applications that runnable using <code>twistd</code>, e.g.
       TAPs or plugins, there is no need to choose a reactor explicitly, since
       this can be chosen using <code>twistd</code>'s -r option.</p>

    <p>In all cases, the event loop is started by calling <code
    class="python">reactor.run()</code>. In all cases, the event loop
    should be stopped with <code
    class="python">reactor.stop()</code>.</p>

    <p><strong>IMPORTANT:</strong> installing a reactor should be the first thing
    done in the app, since any code that does
    <code class="python">from twisted.internet import reactor</code> will automatically
    install the default reactor if the code hasen't already installed one.</p>

    <h2>Reactor Functionality</h2>

    <table cellpadding="7" cellspacing="0" border="1" title="Summary of reactor features">
    <tr><td></td><th>Status</th><th>TCP</th><th>SSL</th><th>UDP</th><th>Threading</th><th>Processes</th><th>Scheduling</th><th>Platforms</th></tr>
    <tr><th>select()</th><td>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>poll()</th><td>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix</td></tr>
    <tr><th>Win32 (WFMO)</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Win32</td></tr>
    <tr><th>Win32 (IOCP)</th><td>Experimental</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td><td>Win32</td></tr>
    <tr><th>CoreFoundation</th><td>Unmaintained</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>OS X</td></tr>
    <tr><th>epoll</th><td>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Linux 2.6</td></tr>
    <tr><th>Gtk</th><td>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>wx</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>kqueue</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>FreeBSD</td></tr>
    </table>

    <h2>General Purpose Reactors</h2>

    <h3>Select()-based Reactor</h3><a name="select" />

    <p>
    The select reactor is currently the default reactor on all platforms.
    The following code will install it, if no other reactor has been
    installed:
    </p>

<pre class="python">
from twisted.internet import reactor
</pre>

    <p>
    In the future, if another reactor becomes the default, but the select
    reactor is desired, it may be installed via:
    </p>

<pre class="python">
from twisted.internet import selectreactor
selectreactor.install()
</pre>


    <h3>Poll()-based Reactor</h3><a name="poll" />

    <p>The PollReactor will work on any platform that provides
    <code class="python">poll()</code>.  With larger numbers of
    connected sockets, it may provide for better performance.</p>

<pre class="python">
from twisted.internet import pollreactor
pollreactor.install()
</pre>

    <h2>Platform-Specific Reactors</h2>

    <h3>KQueue</h3><a name="kqueue" />

    <p>The KQueue Reactor allows Twisted to use FreeBSD's kqueue mechanism for
       event scheduling. See instructions in the <code class="API"
       >twisted.internet.kqreactor</code>'s
       docstring for installation notes.</p>

<pre class="python">
from twisted.internet import kqreactor
kqreactor.install()
</pre>


   <h3>Win32 (WFMO)</h3><a name="win32_wfmo" />

    <p>The Win32 reactor is not yet complete and has various limitations
    and issues that need to be addressed.  The reactor supports GUI integration
    with the win32gui module, so it can be used for native Win32 GUI applications.
    </p>

<pre class="python">
from twisted.internet import win32eventreactor
win32eventreactor.install()
</pre>

   <h3>Win32 (IOCP)</h3><a name="win32_iocp" />

    <p>
    Windows provides a fast, scalable event notification system known as IO
    Completion Ports, or IOCP for short.  An extremely experimental reactor
    based on IOCP is provided with Twisted.
    </p>

<pre class="python">
from twisted.internet import iocpreactor
iocpreactor.install()
</pre>

    <h3>Epoll-based Reactor</h3><a name="epoll" />

    <p>The EPollReactor will work on any platform that provides
    <code class="python">epoll</code>, today only Linux 2.6 and over. The
    implementation of the epoll reactor currently use the Level Triggered
    interface, which is basically like poll() but scales much better.</p>

<pre class="python">
from twisted.internet import epollreactor
epollreactor.install()
</pre>

    <h2>GUI Integration Reactors</h2>

    <h3>GTK+</h3><a name="gtk" />

    <p>Twisted integrates with <a href="http://www.daa.com.au/~james/pygtk/"
    >PyGTK</a>, versions 1.2 (<code>gtkreactor</code>) and 2.0
    (<code>gtk2reactor</code>). Sample applications using GTK+ and
    Twisted are available in the Twisted SVN.</p>

    <p>GTK-2.0 split the event loop out of the GUI toolkit, into a separate
    module called <q>glib</q>. To run an application using the glib event
    loop, use the <code>glib2reactor</code>. This will be slightly faster
    than <code>gtk2reactor</code> (and does not require a working X display),
    but cannot be used to run GUI applications.</p>

<pre class="python">
from twisted.internet import gtkreactor # for gtk-1.2
gtkreactor.install()
</pre>

<pre class="python">
from twisted.internet import gtk2reactor # for gtk-2.0
gtk2reactor.install()
</pre>

<pre class="python">
from twisted.internet import glib2reactor # for non-GUI apps
glib2reactor.install()
</pre>

    <h3>CoreFoundation</h3><a name="cfreactor" />

    <p>Twisted integrates with <a href="http://pyobjc.sf.net/"
    >PyObjC</a>, version 1.0.  Sample applications using Cocoa and Twisted
    are available in the examples directory under <code>Cocoa</code>.</p>

<pre class="python">
from twisted.internet import cfreactor
cfreactor.install()
</pre>

    <h2>Non-Reactor GUI Integration</h2>

    <h3>Tkinter</h3><a name="tkinter" />

    <p>The support for <a href="http://www.python.org/topics/tkinter/"
    >Tkinter</a> doesn't use a specialized reactor.  Instead, there is
    some specialized support code:</p>

<pre class="python">
from Tkinter import *
from twisted.internet import tksupport

root = Tk()

# Install the Reactor support
tksupport.install(root)

# at this point build Tk app as usual using the root object,
# and start the program with "reactor.run()", and stop it
# with "reactor.stop()".
</pre>

    <h3>wxPython</h3><a name="wxpython" />

    <p>Twisted currently supports two methods of integrating
    wxPython. Unfortunately, neither method will work on all wxPython
    platforms (such as GTK2 or Windows). It seems that the only
    portable way to integrate with wxPython is to run it in a separate
    thread. One of these methods may be sufficient if your wx app is
    limited to a single platform.</p>

    <p>As with <a href="#tkinter">Tkinter</a>, the support for integrating
    Twisted with a <a href="http://www.wxpython.org">wxPython</a>
    application uses specialized support code rather than a simple reactor.</p>

<pre class="python">
from wxPython.wx import *
from twisted.internet import wxsupport, reactor

myWxAppInstance = wxApp(0)
wxsupport.install(myWxAppInstance)
</pre>

    <p>However, this has issues when running on Windows, so Twisted now
    comes with alternative wxPython support using a reactor. Using
    this method is probably better. Initialization is done in two
    stages. In the first, the reactor is installed:</p>

<pre class="python">
from twisted.internet import wxreactor
wxreactor.install()
</pre>

    <p>Later, once a <code class="python">wxApp</code> instance has
    been created, but before <code class="python">reactor.run()</code>
    is called:</p>

<pre class="python">
myWxAppInstance = wxApp(0)
reactor.registerWxApp(myWxAppInstance)
</pre>

    <p>An example Twisted application that uses WxWindows can be found
    in <code class="py-filename">doc/examples/wxdemo.py</code>.</p>

    <h3>PyUI</h3><a name="pyui" />

    <p>As with <a href="#tkinter">Tkinter</a>, the support for integrating
    Twisted with a <a href="http://pyui.sourceforge.net">PyUI</a>
    application uses specialized support code rather than a simple reactor.</p>

<pre class="python">
from twisted.internet import pyuisupport, reactor

pyuisupport.install(args=(640, 480), kw={'renderer': 'gl'})
</pre>

    <p>An example Twisted application that uses PyUI can bve found in <code
    class="py-filename">doc/examples/pyuidemo.py</code>.</p>

  </body>
</html>
